package fr.afcepf.ai89.esharing.data;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import fr.afcepf.ai89.esharing.entity.Aem;
import fr.afcepf.ai89.esharing.entity.Avis;
import fr.afcepf.ai89.esharing.entity.Proprietaire;

@Stateless
public class DAOAvis implements IDAOAvis {

	@PersistenceContext(unitName = "ESharingPhase22")
	EntityManager em;

	@Override
	public Avis getAvis(int idAvis) {
		
		Query hql = em.createQuery("SELECT a FROM Avis a WHERE a.idAvis = :param");
		hql.setParameter("param", idAvis);
		
		List<Avis> avs = (List<Avis>)hql.getResultList();
		
		if (avs.size() > 0) {

			return avs.get(0);
			
		}
		
		return null;

	}

	@Override
	public List<Avis> getAvis() {

		Query hql = em.createQuery("SELECT a FROM Avis a");
		List<Avis> av = (List<Avis>)hql.getResultList();
		
		return av;

	}

	@Override
	public List<Avis> getAvis(Proprietaire prop) {
		// TODO Auto-generated method stub
		return null;

	}

	@Override
	public List<Avis> getAvis(Aem aem) {

		// TODO Auto-generated method stub
		return null;

	}

	@Override
	public int addAvis(Avis avis) {
		
		avis.setDateAvis(new Date());
		em.persist(avis);
		
		Query hql = em.createQuery("SELECT MAX(idAvis) FROM Avis a");
		int idAvis = (Integer)hql.getSingleResult();
		
		return idAvis;

	}

	@Override
	public int majAvis(Avis avis) {
		// TODO Auto-generated method stub
		return 0;
	}

}
